/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import java.io.Serializable;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import util.uXML;

/**
 *
 * @author Phạm Huy Đức
 */
public final class mThread implements Serializable{

   private String strTitle, strClass, strStartupState, strLogFile, index, status;
   private Thread bgrThread;

   public mThread() {
      clear();
   }

   @Override
   public mThread clone() {
      mThread rt = new mThread();
      rt.setIndex(index);
      rt.setStrClass(strClass);
      rt.setStrLogFile(strLogFile);
      rt.setStrStartupState(strStartupState);
      rt.setStrTitle(strTitle);
      rt.setBgrThread(bgrThread);
      rt.setStatus(status);
      return rt;
   }

   public void clear() {
      setIndex("");
      setStrClass("");
      setStrLogFile("");
      setStrStartupState("");
      setStrTitle("");
      setBgrThread(null);
      setStatus("0");
   }

   /**
    * @return the strTitle
    */
   public String getStrTitle() {
      return strTitle;
   }

   /**
    * @param strTitle the strTitle to set
    */
   public void setStrTitle(String strTitle) {
      this.strTitle = strTitle;
   }

   /**
    * @return the strClass
    */
   public String getStrClass() {
      return strClass;
   }

   /**
    * @param strClass the strClass to set
    */
   public void setStrClass(String strClass) {
      this.strClass = strClass;
   }

   /**
    * @return the strStartupState
    */
   public String getStrStartupState() {
      return strStartupState;
   }

   /**
    * @param strStartupState the strStartupState to set
    */
   public void setStrStartupState(String strStartupState) {
      this.strStartupState = strStartupState;
   }

   /**
    * @return the strLogFile
    */
   public String getStrLogFile() {
      return strLogFile;
   }

   /**
    * @param strLogFile the strLogFile to set
    */
   public void setStrLogFile(String strLogFile) {
      this.strLogFile = strLogFile;
   }

   /**
    * @return the index
    */
   public String getIndex() {
      return index;
   }

   /**
    * @param index the index to set
    */
   public void setIndex(String index) {
      this.index = index;
   }

   /**
    * @return the bgrThread
    */
   public Thread getBgrThread() {
      return bgrThread;
   }

   /**
    * @param bgrThread the bgrThread to set
    */
   public void setBgrThread(Thread bgrThread) {
      this.bgrThread = bgrThread;
   }

   public String outStringX() {
      //Hàm lấy thông tin trạng thái tiến trình qua String      
//   private String strTitle, strClass, strStartupState, strLogFile, index;
//   private Thread bgrThread;
      StringBuilder rtStr = new StringBuilder("<root>");
      rtStr.append("<strTitle>");
      rtStr.append(strTitle);
      rtStr.append("</strTitle>");
      rtStr.append("<strClass>");
      rtStr.append(strClass);
      rtStr.append("</strClass>");
      rtStr.append("<strStartupState>");
      rtStr.append(strStartupState);
      rtStr.append("</strStartupState>");
      rtStr.append("<strLogFile>");
      rtStr.append(strLogFile);
      rtStr.append("</strLogFile>");
      rtStr.append("<index>");
      rtStr.append(index);
      rtStr.append("</index>");
      rtStr.append("<status>");
      if (getBgrThread().isAlive()) {//Nếu đang sống thì gán =1
         rtStr.append("1");
      } else if (getBgrThread().isInterrupted()) {//Nếu đang tắt thì gán =2
         rtStr.append("2");
      } else {//Nếu không thì coi như chết rồi
         rtStr.append("0");
      }
      rtStr.append("");
      rtStr.append("</status>");
      rtStr.append("</root>");
      return rtStr.toString();
   }

   public void inStringX(String inStr) {
      //Hàm import dữ liệu vào từ nội dung XML
      Document inDoc = uXML.loadXMLFromString(inStr);
      inDoc.getDocumentElement().normalize();
      //Chuẩn hóa
//      NodeList nodes = inDoc.getElementsByTagName("root");
      clear();
      setIndex(inDoc.getElementsByTagName("index").item(0).getTextContent());
      setStrClass(inDoc.getElementsByTagName("strClass").item(0).getTextContent());
      setStrLogFile(inDoc.getElementsByTagName("strLogFile").item(0).getTextContent());
      setStrStartupState(inDoc.getElementsByTagName("strStartupState").item(0).getTextContent());
      setStrTitle(inDoc.getElementsByTagName("strTitle").item(0).getTextContent());
      setStatus(inDoc.getElementsByTagName("status").item(0).getTextContent());
   }

   /**
    * @return the status
    */
   public String getStatus() {
      return status;
   }

   /**
    * @param status the status to set
    */
   public void setStatus(String status) {
      this.status = status;
   }
}